package com.dodo.blog.ui.ajax;

import com.dodo.blog.RequestCycle;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class AjaxListenerHelper
{
    private static final String XML_HTTP_REQUEST = "XMLHttpRequest";

    private static final String X_REQUESTED_WITH = "X-Requested-With";

    public static final String PARAM_MODEL_ID = "__model_id";

    public static final String PARAM_METHOD = "__method";

    public static final String PARAM_METHOD_MODEL = "__model";

    public static final String PARAM_JSON = "__json";

    public static void addListener( AjaxListener listener )
    {
        AjaxListener al = getListener( listener );
        if ( al == null )
        {
            setListener( listener );
        }
    }

    public static void setListener( AjaxListener listener )
    {
        RequestCycle.get().getSession().setAttribute( listener.getUniqueId(), listener );
    }

    public static AjaxListener getListener( AjaxListener listener )
    {
        return ( AjaxListener ) RequestCycle.get().getSession().getAttribute( listener.getUniqueId() );
    }

    public static boolean isAjax()
    {
        return XML_HTTP_REQUEST.equals( RequestCycle.get().getRequest().getHeader( X_REQUESTED_WITH ) );
    }

}


